/**
 * Created by Sergey on 26.11.13.
 */

var oldCell = null;

function deactivateCell(cell) {
    //var field = $('input', cell);
    var field = $('textarea', cell);

    cell.text(field.val());
    cell.removeClass('cell-active');
    field.remove();
}

//
function createExcelTable(selector) {
    $('tbody tr td:not(.cell-active)', selector).click(function () {
        $('#log').append('Select new cell <br>');

        var cell = $(this);
        var content = $(this).text();
        cell.text('');
//        cell.append('<input type="text" value="' + content + '"/>');
        cell.append('<textarea>' + content + '</textarea>');
        cell.addClass('cell-active');

        if (oldCell != null && oldCell != cell) {
            deactivateCell(oldCell);
        }
        oldCell = cell;

        $('input', cell).focus().focusout(function () {
            deactivateCell(cell);
        });
    });
}

$(function () {
    createExcelTable('.excel-table');
});